package com.gomore.opple.module.main.good.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventIsExpend;
import com.gomore.opple.model.Filter;
import com.gomore.opple.widgets.MyGridView;
import com.gomore.opple.widgets.adapter.CommonAdapter;
import com.gomore.opple.widgets.adapter.ViewHolder;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by asus on 2017/5/12.
 */
public class PopRecyclerViewAdapter extends CommonAdapter<Filter> {
    private Activity mActivity;

    public PopRecyclerViewAdapter(Activity activity, int layoutId, List<Filter> datas) {
        super(activity, layoutId, datas);
        this.mActivity = activity;
    }

    @Override
    protected void convert(ViewHolder holder, final Filter filter, int position) {
        TextView title = holder.getView(R.id.title);
        RelativeLayout relativeLayout = holder.getView(R.id.more_relative);
        LinearLayout linearLayout = holder.getView(R.id.more_liner);
        ImageView arrow = holder.getView(R.id.arrow);
        MyGridView myGridView = holder.getView(R.id.my_grid_view);
        //判断是否展开
        if (filter.isExpend()) {
            arrow.setImageResource(R.mipmap.arrow_up);
            myGridView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);
        } else {
            arrow.setImageResource(R.mipmap.arrow_down);
            myGridView.setVisibility(View.GONE);
            if (!filter.isFirstCategory()) {
                relativeLayout.setVisibility(View.GONE);
            } else {
                relativeLayout.setVisibility(View.VISIBLE);
            }
        }

        if (filter.getCategory() != null) {
            title.setText(filter.getCategory());
        }
        if (filter.isFirstCategory()) {
            title.setTextSize(16);
            title.setTextColor(mActivity.getResources().getColor(R.color.black));
            linearLayout.setVisibility(View.VISIBLE);
        } else {
            title.setTextSize(15);
            title.setTextColor(mActivity.getResources().getColor(R.color.blue));
            linearLayout.setVisibility(View.GONE);
        }
        FilerListAdapter adapter = new FilerListAdapter(mActivity, filter.getSecondFilterList());
        myGridView.setAdapter(adapter);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setIsExpend(filter);
            }
        });
    }

    //设置张开
    private void setIsExpend(Filter filter) {
        //不是第一层title,没有点击事件
        if (!filter.isFirstCategory()) {
            return;
        }
        if (filter.isExpend()) {
            filter.setIsExpend(false);
            if (filter.getCategory().equals(GlobalConstant.FilterCategory.CATEGORIES)) {
                EventIsExpend eventIsExpend = new EventIsExpend();
                eventIsExpend.setIsExpend(false);
                EventBus.getDefault().post(eventIsExpend);
            } else {
                notifyDataSetChanged();
            }
        } else {
            filter.setIsExpend(true);
            if (filter.getCategory().equals(GlobalConstant.FilterCategory.CATEGORIES)) {
                EventIsExpend eventIsExpend = new EventIsExpend();
                eventIsExpend.setIsExpend(true);
                EventBus.getDefault().post(eventIsExpend);
            } else {
                notifyDataSetChanged();
            }
        }
    }

}
